Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

143
Visualizações
Javascript regex (x|y) - Can it be specified by array.join('|')?

I'm trying to parse text obtained from the Wikipedia API.

In detail, I'm trying to parse the wikitext of a whole wiki page into separate sections. On wikipedia, sections are divided by titles enclosed by 2-5 equal symbols (e.g. == TITLE ==). So the whole wikitext of a page has a bunch of this, and I was thinking I'd be able to get the content of each section using split() and regex. Here's my naive try:

let wikitext = text; // Obtained from the API
let sectiontitles = [sectiontitle1, sectiontitle2...]; // Obtained from the API
let sectionsPiped = sectiontitles.join('|');
let regex = new RegExp(`\\={2,5}\\s*(${sectionsPiped})\\s*\\={2,5}`, 'g');
console.log(wikitext.split(regex));

This worked, but not the way I expected. Let's say the wikitext has the following contour (replacing \n with actual linebreaks for readability's sake):

== Section 1 ==
SECTION-1-TEXT
=== Section 1.1 ===
SECTION-1.1-TEXT
=== Section 1.2 ===
SECTION-1.2-TEXT

Then the code above returns:

0: ''
1: 'Section 1'
2: 'SECTION-1-TEXT'
3: 'Section 1.1'
4: 'SECTION-1.1-TEXT'
5: 'Section 1.2'
6: 'SECTION-1.2-TEXT'

but I was expecting:

0: ''
1: 'SECTION-1-TEXT'
2: 'SECTION-1.1-TEXT'
3: 'SECTION-1.2-TEXT'

I guess I'm doing something wrong with the (x|y) regex, so I need your help with this.

Note that API:Parsing_wikitext can parse a single section, but not multiple sections (for this reason I'd have to iterate API requests if I try to do this with the API, but I want to avoid this because the code will otherwise be slow). And in the end, I need to get an array of the contents of each section INCLUDING the title headers like so:

0: ''
1: '== Section 1 ==\nSECTION-1-TEXT'
2: '=== Section 1.1 ===\nSECTION-1.1-TEXT'
3: '=== Section 1.2 ===\nSECTION-1.2-TEXT'

I can do this by adding the headers to the second last array above after spliting the whole wikitext, but is there any easier way to do this? Any help would be appreciated.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda